<?php

declare(strict_types=1);

namespace app\common\service;
use think\facade\Session;
use think\facade\Cookie;
use think\facade\Request;
use app\common\model\AdminUser as M;
use app\common\validate\AdminUser as V;

class AdminUser
{
    // 添加
    public static function goAdd($data)
    {
        //验证
        $validate = new V;
        if (!$validate->scene('add')->check($data))
            return ['msg' => $validate->getError(), 'code' => 201];
        $ishan = M::findbyopenid($data["openid"]);
        if ($ishan) return ['msg' => '用户已存在', 'code' => 202];
        try {
            M::create($data);
            return ['msg' => '添加成功', 'code' => 200];
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }
    // 新增并登陆如存在直接走登录
    public static function wxlogin($data)
    {
        //验证
        $validate = new V;
        if (!$validate->scene('add')->check($data))
            return ['msg' => $validate->getError(), 'code' => 201];
        $umd = M::findbyopenid($data["openid"]);
        if ($umd) 
        {
            $umd->xftoken = rand_string().$umd->id.microtime(true);
            $umd->save();
            $info = [
                'id'=>$umd->id,
                'openid' => $umd->openid,
                'nickname'=> urldecode($umd->nickname),
                'headimgurl'=>$umd->headimgurl,
                'xftoken' => $umd->xftoken
            ];
            Session::set('xfuser', $info);
            Cookie::set('xftoken',$umd->xftoken, 3600);
            return ['msg' => '用户已存在', 'code' => 202];
        }
        try {
            $umd = M::create($data);
            $umd->xftoken = rand_string().$umd->id.microtime(true);
            $umd->save();
            $info = [
                'id'=>$umd->id,
                'nickname'=>urldecode($umd->nickname),
                'openid' => $umd->openid,
                'headimgurl'=>$umd->headimgurl,
                'xftoken' => $umd->xftoken
            ];
            Session::set('xfuser', $info);
            Cookie::set('xftoken',$umd->xftoken, 3600);
            return ['msg' => '添加成功', 'code' => 200];
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }
      // 判断是否登录
      public static function isLogin()
      {
          if(Session::get('xfuser')) return true; 
          if(Cookie::has('xftoken')){
              $umd = M::where(['xftoken'=>Cookie::get('xftoken'),'status'=>1])->find();
              if(!$umd) return false;
              return Session::set('xfuser',[
                'id'=>$umd->id,
                'nickname'=>urldecode($umd->nickname),
                'openid' => $umd->openid,
                'headimgurl'=>$umd->headimgurl,
                'xftoken' => $umd->xftoken
              ]); 
          }
          return false;
      }
      
    // 编辑
    public static function goEdit($data, $id)
    {
        $data['id'] = $id;
        //验证
        $validate = new V;
        if (!$validate->scene('edit')->check($data))
            return ['msg' => $validate->getError(), 'code' => 201];
        try {
            M::update($data);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }
    public static function address($data,$id)
    {
        $data['id'] = $id;
        //验证
        try {
            M::update($data);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function hasaddress($id)
    {
        $model =  M::find($id);
        if ($model->isEmpty()) return false;
        if(!$model->address) return false;
        return true;
    }
    // 状态
    public static function goStatus($data, $id)
    {
        $model =  M::find($id);
        if ($model->isEmpty())  return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save([
                'status' => $data,
            ]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    // 删除
    public static function goRemove($id)
    {
        $model = M::find($id);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->delete();
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    // 批量删除
    public static function goBatchRemove($ids)
    {
        if (!is_array($ids)) return ['msg' => '数据不存在', 'code' => 201];
        try {
            M::destroy($ids);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    // 获取列表
    public static function goRecycle()
    {
        if (Request::isPost()) {
            $ids = Request::param('ids');
            if (!is_array($ids)) return ['msg' => '参数错误', 'code' => '201'];
            try {
                if (Request::param('type')) {
                    $data = M::onlyTrashed()->whereIn('id', $ids)->select();
                    foreach ($data as $k) {
                        $k->restore();
                    }
                } else {
                    M::destroy($ids, true);
                }
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
            return ['msg' => '操作成功'];
        }
        //按用户名
        $where = [];
        $limit = input('get.limit');

        //按openid查找
        if ($openid = input("openid")) {
            $where[] = ["openid", "like", "%" . $openid . "%"];
        }
        //按昵称查找
        if ($nickname = input("nickname")) {
            $where[] = ["nickname", "like", "%" . $nickname . "%"];
        }
        //按是否启用查找
        if ($status = input("status")) {
            $where[] = ["status", "like", "%" . $status . "%"];
        }
        $list = M::onlyTrashed()->where($where)->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }
}
